package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityAdvCost;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Utils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.loongtech.bi.manager.system.SysProjectManager.projectIdNameMap;

@Service
public class AdvCostManager extends ManagerQueryCacheBase<EntityAdvCost>  {

	private static final long serialVersionUID = 2L;

    /**
     * 查询任务(springmvc)
     */
    public Page<List<EntityAdvCost>> queryByCriteria(Integer curPage, Integer pageSize, EntityAdvCost entity) {
        List<Object> cons = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityAdvCost l where 1=1 ");
        if (!Utils.isEmptyStr(entity.getLdate())) {
            hql.append(" and l.ldate = ? ");
            cons.add(entity.getLdate());
        }
        if (!Utils.isEmptyStr(entity.getPlatform())) {
            hql.append(" and l.platform = ? ");
            cons.add(entity.getPlatform());
        }
        if (!Utils.isEmptyStr(entity.getAgent())) {
            hql.append(" and l.agent = ? ");
            cons.add(entity.getAgent());
        }
        if (!Utils.isEmptyStr(entity.getChannelName())) {
            hql.append(" and l.channelName = ? ");
            cons.add(entity.getChannelName());
        }
        if (!Utils.isEmptyStr(entity.getCost())) {
            hql.append(" and l.cost = ? ");
            cons.add(entity.getCost());
        }
        if (!Utils.isEmptyStr(entity.getRebate())) {
            hql.append(" and l.rebate = ? ");
            cons.add(entity.getRebate());
        }
        if (!Utils.isEmptyStr(entity.getDiscount())) {
            hql.append(" and l.discount = ? ");
            cons.add(entity.getDiscount());
        }
        if (!Utils.isEmptyStr(entity.getRoi())) {
            hql.append(" and l.roi = ? ");
            cons.add(entity.getRoi());
        }
        Integer totalSize = countNum(hql.toString(), cons.toArray());
        //数据
        List<EntityAdvCost> list = findByQuery((curPage - 1) * pageSize, pageSize, hql.toString(), cons.toArray());
        if (list != null && list.size() > 0) {
            for (EntityAdvCost entityAdv : list) {
                entityAdv.setGameName(projectIdNameMap.get(entityAdv.getGameId().toString()));
            }
        }
        return new Page<>(list, totalSize, curPage, pageSize);
    }
}
